package cn.com.panda.view.camera;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import cn.com.panda.R;

/**
 * created by liujunlin on 2022/3/15 10:50
 */
public class ProgressCameraView extends ImageView {

    private boolean isCapture;
    private int progress;
    private int maxProgress;
    private Context mContext;
    private int startAngle = -90;
    private float mProgressWidth;//进度条宽度

    public ProgressCameraView(Context context) {
        super(context);
        mContext = context;
    }

    public ProgressCameraView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public boolean isCapture() {
        return isCapture;
    }

    public void setCapture(boolean capture) {
        isCapture = capture;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        startAngle = startAngle + 360/maxProgress;
        invalidate();
    }

    public void setMaxProgress(int maxProgress) {
        this.maxProgress = maxProgress;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        mProgressWidth = 10;

        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();

        int centerX = (width - getPaddingLeft() - getPaddingRight())/2;
        int centerY = (height - getPaddingTop() - getPaddingBottom())/2;
        int radius = Math.min(width,height)/2;

        RectF mRectF = new RectF();
        mRectF.left = 0; // 左上角x
        mRectF.top = 0; // 左上角y
        mRectF.right = width; // 左下角x
        mRectF.bottom = height; // 右下角y

        Paint circlePaint = new Paint();
        circlePaint.setAntiAlias(true);
        circlePaint.setColor(mContext.getColor(R.color.capture_btn_bg));
        circlePaint.setStyle(Paint.Style.FILL);
        circlePaint.setStrokeCap(Paint.Cap.ROUND);
        canvas.drawCircle(centerX,centerY,radius-20,circlePaint);
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setStrokeCap(Paint.Cap.ROUND);
        circlePaint.setStrokeWidth(10);
        canvas.drawCircle(centerX,centerY,radius-10,circlePaint);

        if(isCapture){
            RectF rectF = new RectF(mProgressWidth / 2, mProgressWidth / 2, getWidth() - mProgressWidth / 2, getHeight() - mProgressWidth / 2);
            Paint circleOutline = new Paint();
            circleOutline.setStyle(Paint.Style.STROKE);
            circleOutline.setStrokeWidth(10f);
            circleOutline.setAntiAlias(true);
            circleOutline.setColor(Color.WHITE);
            circleOutline.setStrokeCap(Paint.Cap.ROUND);
            canvas.drawArc(rectF, -90, 360, false, circleOutline);
            circleOutline.setColor(Color.argb(255,65,236,13));
            canvas.drawArc(rectF,-90,((float) progress / maxProgress) * 360,false,circleOutline);

            Paint textPaint = new Paint();
            textPaint.setColor(Color.argb(255,65,236,13));
            textPaint.setStrokeWidth(12);
            textPaint.setTextSize(50);
            textPaint.setTextAlign(Paint.Align.CENTER);
            //计算baseline
            Paint.FontMetrics fontMetrics=textPaint.getFontMetrics();
            float distance=(fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
            float baseline=centerY+distance;
            canvas.drawText(progress + "/" + maxProgress,centerX,baseline,textPaint);
        }
        else{
            Bitmap src = BitmapFactory.decodeResource(getResources(),R.drawable.cap);
            Rect srcRect = new Rect(0,0,src.getWidth(),src.getHeight());
            Rect descRect = new Rect(centerX - radius/2,centerY-radius/2,centerX + radius/2,centerY+radius/2);
//            canvas.drawBitmap(src,srcRect,descRect,null);
        }
    }
}
